package com.xuexiang.xuidemo.page.uilts.component;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;

import com.xuexiang.xui_lib.util.OfAnimatorProperty;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.page.base.BasePageAbilitySlice;
import com.xuexiang.xuidemo.page.base.Page;

/**
 * 控件自定义动画
 *
 * @since 2021-03-25
 */
@Page(name = "控件自定义动画", action = "utils.CustomWidgetAnimationAbilitySlice")
public class CustomWidgetAnimationAbilitySlice extends BasePageAbilitySlice implements Component.ClickedListener {
    private static final int DURATION = 500;
    private static final int ROTATE_45 = 45;
    private Component target;
    private Component start;

    private boolean isExpand;

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_ability_utils_custom_widget_animation;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        target = findComponentById(ResourceTable.Id_target);
        start = findComponentById(ResourceTable.Id_iv_start);
        start.setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {
        if (component.getId() == start.getId()) {
            switchAnimation();
        }
    }

    /**
     * 切换旋转动画
     */
    private void switchAnimation() {
        long duration = DURATION;
        OfAnimatorProperty targetAnimator = new OfAnimatorProperty(target).setDuration(duration);
        OfAnimatorProperty startAnimator = new OfAnimatorProperty(start).setDuration(duration);
        if (isExpand) {
            targetAnimator.alpha(1, 0);
            startAnimator.rotate(-ROTATE_45, 0);
        } else {
            targetAnimator.alpha(0, 1);
            startAnimator.rotate(0, -ROTATE_45);
        }
        targetAnimator.start();
        startAnimator.start();
        isExpand = !isExpand;
    }
}
